package com.geek.jdbc.utils;

import java.sql.*;

/**
 * @Desc TODO
 * @Author
 * @Date 2025/11/17
 */
public class JDBCUtils {

    //获取连接对象
    public static Connection getConn(){
        //1.加载驱动类
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.通过驱动管理器获取连接对象
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/testsqlgeek?useUnicode=true&charsetEncoding=UTF8&serverTimezone=Asia/Shanghai",
                    "root", "094115");
            return conn;
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //关闭资源
    public static void close(Statement st , Connection conn , ResultSet rs) {
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(st!=null){
            try {
                st.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
